//
//  ZZCollectionViewController.m
//  zhengzai-tv
//
//  Created by SC on 16/6/24.
//  Copyright © 2016年 zhengzai.tv. All rights reserved.
//

#import "ZZCollectionViewController.h"
#import "ZZTableViewModel.h"
#import "RZDataBinding.h"

@interface ZZCollectionViewController ()

@property (nonatomic, weak, readwrite) IBOutlet UICollectionView *collectionView;
@property (nonatomic, strong) ZZTableViewModel *viewModel;

@end

@implementation ZZCollectionViewController

@synthesize viewModel = _viewModel;

- (void)viewDidLoad {
    [super viewDidLoad];
    self.collectionView.contentInset  = self.contentInset;
    self.collectionView.scrollIndicatorInsets = self.contentInset;
    
    self.collectionView.backgroundColor = [UIColor clearColor];
    
    if ([self.viewModel shouldRequestRemoteDataOnViewDidLoad]) {
        [self.viewModel requestRemoteData];
    }
}

- (void)bindViewModel
{
    [super bindViewModel];
    [self.viewModel rz_addTarget:self action:@selector(reloadData:) forKeyPathChange:@"dataSource"];
}

- (void)reloadData:(id)change
{
    [self.collectionView reloadData];
}

#pragma mark - UICollectionViewDataSource
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return self.viewModel.dataSource ? [self.viewModel.dataSource count] : 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return [self.viewModel.dataSource[section] count];
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return nil;
}

#pragma mark - UICollectionViewDelegate
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    [collectionView deselectItemAtIndexPath:indexPath animated:YES];
    [self.viewModel didSelectRowAtIndexPath:indexPath];
}

#pragma mark - DZNEmptyDataSetSource
- (NSAttributedString *)titleForEmptyDataSet:(UIScrollView *)scrollView {
    return [[NSAttributedString alloc] initWithString:@"No Data"];
}

#pragma mark - DZNEmptyDataSetDelegate
- (BOOL)emptyDataSetShouldDisplay:(UIScrollView *)scrollView {
    return self.viewModel.dataSource == nil;
}

- (BOOL)emptyDataSetShouldAllowScroll:(UIScrollView *)scrollView {
    return YES;
}

- (CGPoint)offsetForEmptyDataSet:(UIScrollView *)scrollView {
    return CGPointMake(0, -(self.collectionView.contentInset.top - self.collectionView.contentInset.bottom) / 2);
}

@end
